package com.example.reactor.rsocket.config;

import com.example.reactor.rsocket.config.entity.TimeSeriesInfo;
import com.google.common.base.CaseFormat;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.messaging.rsocket.RSocketStrategies;
import org.springframework.util.MimeTypeUtils;

/**
 * @author zpMeng
 * @since 10/11/2024
 */
@Configuration
public class ReactiveServerConfig {

    @Bean
    public RSocketStrategies rSocketStrategies() {

        CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, TimeSeriesInfo.class.getSimpleName());

        return RSocketStrategies.builder()
                .metadataExtractorRegistry(metadataExtractorRegistry -> {
                    // 注册自定义 MIME 类型的元数据提取
                    metadataExtractorRegistry.metadataToExtract(
                            MimeTypeUtils.parseMimeType("application/json"),
                            TimeSeriesInfo.class,
                            CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, TimeSeriesInfo.class.getSimpleName()));
                })
                .decoder(new Jackson2JsonDecoder())
                .encoder(new Jackson2JsonEncoder())
                .build();
    }
}
